package com.snack.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snack.common.result.Result;
import com.snack.entity.pojo.Category;
import com.snack.mapper.CategoryMapper;
import com.snack.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huangfuhao
 * @Date 2025/3/28
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Override
    public Result addCategory(Category category) {
        Category one = lambdaQuery().eq(Category::getName, category.getName()).one();
        if (one !=null){
            return Result.error("添加失败，该分类已存在");
        }
        boolean save = save(category);
        if (!save){
            return Result.error("添加失败，请查看后台");
        }
        return Result.success(category);
    }

    @Override
    public Result deleteCategory(Category category) {
        boolean b = removeById(category);
        if (!b){
            return Result.error("删除失败");
        }
        return Result.success();
    }

    @Override
    public Result listCategory() {
        List<Category> list = list();

        if (list == null || list.isEmpty()) {
            return Result.success(new ArrayList<>());
        }

        return Result.success(list);
    }
}